/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using DaveSexton.DocProject.Configuration;

namespace DaveSexton.DocProject
{
	internal sealed class DocProjectPropertiesToolWindow : ToolWindow
	{
		#region Public Properties
		public static readonly Guid ToolWindowGuid = new Guid("{FDA3F750-F0BF-4487-804B-120FF5BF19A3}");

		public static readonly DocProjectPropertiesToolWindow Instance = new DocProjectPropertiesToolWindow();

		public override string Caption
		{
			get
			{
				if (project == null)
					return string.Empty;
				else
					return string.Format(System.Globalization.CultureInfo.CurrentCulture, 
						Resources.Text.DocProjectPropertiesCaption, project.Name);
			}
		}

		public override System.Windows.Forms.Control Control
		{
			get
			{
				if (control == null)
				{
					control = new DocProjectPropertiesControl();
					
					if (project != null)
						control.Project = project;
				}

				return control;
			}
		}

		public override System.Drawing.Bitmap Image
		{
			get
			{
				return Resources.Images.DocProjectProperties;
			}
		}

		public IDocProject Project
		{
			set
			{
				if (project == value)
					return;

				project = value;

				if (control != null)
					control.Project = project;

				Window.Caption = Caption;
			}
		}
		#endregion

		#region Private / Protected
		private DocProjectPropertiesControl control;
		private IDocProject project;
		#endregion

		#region Constructors
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline")]
		static DocProjectPropertiesToolWindow()
		{
			DocProjectEnvironment.ProjectClosed += DocProjectEnvironment_ProjectClosed;
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="DocProjectPropertiesToolWindow" /> class.
		/// </summary>
		private DocProjectPropertiesToolWindow()
			: base(ToolWindowGuid)
		{
		}
		#endregion

		#region Event Handlers
		private static void DocProjectEnvironment_ProjectClosed(object sender, DocProjectEventArgs e)
		{
			IDocProject project = e.Project;

			if (project != null && Instance.project != null && project.Matches(Instance.project))
				Instance.Visible = false;
		}
		#endregion
	}
}
